package net.minecraft.src;

import java.util.*;

public class BlockReceiver extends  BlockContainer
{
	public static ArrayList<Coords> receiversToUpdate =  new ArrayList<Coords>();
	

	
	public  static Map<String, ArrayList<Coords>> codeToCoords = new HashMap<String, ArrayList<Coords>>();
	
	public  static HashMap<Coords, String> coordsToCodes = new HashMap<Coords, String>();
	
	private  boolean activePower;
	

	private static String stampKey[]= null;
    public int getRenderBlockPass()
    {
        return 0;
    }
    
    public void onBlockAdded(World world, int i, int j, int k)
    {
    	
        if (world.getBlockMetadata(i, j, k) == 0)
        {
            super.onBlockAdded(world, i, j, k);
        }
        world.scheduleBlockUpdate(i, j, k, blockID, tickRate());
        if(stampKey != null)
        {
        	
        	TileRecTransStamp tileReceiverTransmiter = (TileRecTransStamp)world.getBlockTileEntity(i, j, k);
        	tileReceiverTransmiter.setTileString(stampKey);

        	stampKey = null;
        }
    }
 
    public void onBlockRemoval(World world, int i, int j, int k)
    {
    	super.onBlockRemoval(world, i, j, k);
    	removal( world,i, j, k);
    }
    
    public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
    {
       	TileRecTransStamp tileReceiverTransmiter = (TileRecTransStamp)world.getBlockTileEntity(i, j, k);

        ModLoader.openGUI(entityplayer, new GuiEditReceiverStamp(getClass(),tileReceiverTransmiter,world,new Coords(i, j, k))); 
        

        return true;
    }
    
    
    public void setPowerState(World world,Coords updated)
    {
    		if(receiversToUpdate.contains(updated))
    		{
    			receiversToUpdate.remove(updated);
    			int newBlockId =
    					this.blockID == mod_AsPj.blockReceiverIdle.blockID ? mod_AsPj.blockReceiverActive.blockID : mod_AsPj.blockReceiverIdle.blockID;

    	       	TileRecTransStamp tileReceiverTransmiter = (TileRecTransStamp)world.getBlockTileEntity(updated.x, updated.y, updated.z);
    	       	stampKey = tileReceiverTransmiter.getTileStringArray();
    			
    			world.setBlockAndMetadataWithNotify(updated.x, updated.y, updated.z, newBlockId , world.getBlockMetadata(updated.x, updated.y, updated.z));
       		}

    }
    
    public TileEntity getBlockEntity()
    {
        try
        {

            return (TileEntity)new TileRecTransStamp();
        }
        catch (Exception exception)
        {
            throw new RuntimeException(exception);
        }
    }
    
    protected BlockReceiver(int i, int j, boolean  power)
    {
        super(i, j,Material.circuits);
        
        activePower = power;
    }
    
    public void updateTick(World world, int i, int j, int k, Random random)
    {
    	
   	 TileRecTransStamp tileReceiverTransmiter = (TileRecTransStamp)world.getBlockTileEntity(i, j, k);
   	 String  code = tileReceiverTransmiter.getTileString();
        addToMap(i, j, k,code);

        setPowerState(world,new Coords(i,j,k));
 
    }
    
    public int tickRate()
    {
	    return  1;
    }
    
    static public  void  updateAllReceivers(World world, boolean change )
    {

    }

    public boolean renderAsNormalBlock()
    {
        return false;
    }


    
    public boolean isPoweringTo(IBlockAccess iblockaccess, int i, int j, int k, int l)
    {
        return activePower;
    }
    
    public  void removal(World world,int i, int j, int k)
    {
    	
     	 TileRecTransStamp tileReceiverTransmiter = (TileRecTransStamp)world.getBlockTileEntity(i, j, k);
     	 
     	 if( null == tileReceiverTransmiter )
     		 return;
       	 String  code = tileReceiverTransmiter.getTileString();
   	 Coords tempCoords = new  Coords(i, j, k);
   	 // trzecia lista 
   	 if( coordsToCodes.containsKey(tempCoords) )
   	 {
   	     coordsToCodes.remove(tempCoords);
   	 }
	 if( codeToCoords.containsKey(code) )
	 {
	     List<Coords> codeCoords= codeToCoords.get(code);
	     if (codeCoords.contains(tempCoords))
	     {
	         codeCoords.remove(tempCoords);
	     }
	 }
    }
 
    public static void  addToMap(int i, int j, int k,String  code )
    {
    	
    	 Coords tempCoords = new  Coords(i, j, k);

    	 if( !coordsToCodes.containsKey(tempCoords) )
    	 {
    	     coordsToCodes.put(tempCoords,  code);
    	 }
    	 else
    	 {
	    	 String previousCode = coordsToCodes.get(tempCoords);
	    	 if(!previousCode.equals( code ))
	    	 {
		    	         List<Coords> codeCoordsList= codeToCoords.get(previousCode);
		    	         if(codeCoordsList != null)
		    	        	 codeCoordsList.remove(tempCoords);

		    	         coordsToCodes.remove(tempCoords);
		    	         coordsToCodes.put(tempCoords,  code);
	    	 }
    	}
    	 if( codeToCoords.containsKey(code) )
    	 {
    	     List<Coords> codeCoords= codeToCoords.get(code);
    	     if (!codeCoords.contains(tempCoords))
    	     {
    	         codeCoords.add(tempCoords);
    	     }
    	 }
    	 else
    	 {
    		 ArrayList<Coords> arrayOfCoords = new ArrayList<Coords>();
    		 arrayOfCoords.add(tempCoords);
    	     codeToCoords.put(code,arrayOfCoords);
    	 }
    	
    }
    /*public int idDropped(int i, Random random, int j)
    {
        return mod_AsPj.blockReceiverIdle;
    }
*/


    public int idDropped(int i, Random random, int j)
    {
        return mod_AsPj.blockReceiverIdle.blockID;
    }
    
    public void HandlePacket(Packet230ModLoader packet)
    {
            switch(packet.packetType)
            {
                    case 0:
                    {
                            //do some stuff with the packet
                            //you can use packet.dataFloat[] etc
                    }
            }

    }
    
    public boolean isOpaqueCube()
    {
        return false;
    }
}